package com.lzx;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class AutoClick {
    public static void main(String[] args) {
        try{
            while (true){
                String url = "http://www.baidu.com/link?url=GYDo920GT654uMxzrukwLtBQR7QqqNRJo7rObbSXxry8EjKLwbtQ8eC96QO8DMgsYk4gKBYV0kKFbieYY3Gz4MacYhg6Fhs-qJ8cjUj-zdS";
                String html = getHtmlByHttpClient(url);
                System.out.println(html);
                Thread.sleep(1000*60);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static String getHtmlByHttpClient(String url){
        String html = "";
        BufferedReader bufferedReader = null;
        InputStream in = null;
        try{

            HttpGet get = new HttpGet(url);
            get.setHeader("user-agent"," Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");

//            String ip = "218.66.253.146";
//            int port = 8800;
//            HttpHost proxy = new HttpHost(ip,port);
//            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
//            get.setConfig(config);

            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpResponse response = httpClient.execute(get);

            if(response.getStatusLine().getStatusCode() == 200) {
                html = "I get it";
//                HttpEntity entity = response.getEntity();
//
//                in = entity.getContent();
//
//                bufferedReader = new BufferedReader(new InputStreamReader(in));
//                StringBuffer result = new StringBuffer();
//                String line = null;
//                while((line = bufferedReader.readLine()) != null){
//                    result.append(line);
//                    result.append("\n");
//                }
//                html = result.toString();
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(bufferedReader != null){
                    bufferedReader.close();
                }
                if(in != null){
                    in.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return html;
    }


    public static String getHtmlByHttpURLConnection(String urlString){
        StringBuffer stringBuffer = new StringBuffer();
        if(!urlString.startsWith("http")){
            urlString = "https://" + urlString;
        }
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        HttpURLConnection httpURLConnection = null;
        try{
            URL url = new URL(urlString);
            httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            httpURLConnection.setReadTimeout(2000);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(2000);
            if(httpURLConnection.getResponseCode() == 200){
                inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream(),"utf-8");

                bufferedReader = new BufferedReader(inputStreamReader);

                String line = "";

                while((line = bufferedReader.readLine()) != null ){
                    stringBuffer.append(line);
                    stringBuffer.append("\n");
                }
            }else{
                System.out.println(httpURLConnection.getResponseCode());
            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{

                if (bufferedReader != null){
                    bufferedReader.close();
                }
                if(inputStreamReader != null){
                    inputStreamReader.close();
                }
            }catch (IOException ee){
                ee.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }



}

